<?php

class Admin extends Controller {

	public $layout = "admin";
	const REFERENCE_BASE = "resource/image/sta-uradismo/";

	public static function loadReferenceImages(){
		$images = array();
		$dir = opendir(self::REFERENCE_BASE);
		while($f = readdir($dir)){
			if(preg_match('/\.jpe?g$|\.gif$|\.png$/', $f)){
				$images[] = $f;
			}
		}
		natsort($images);
		return $images;
	}
	
	public function index(){
		$this->title = "ADMINISTRACIJA";
	}
	
	public function login(){
		Security::login($this->HTTP->username, $this->HTTP->password);
		redirect("/admin");
	}
	public function logout(){
		Security::logout();
		redirect("/admin");
	}
	
	public function settings(){
		$cfg = $this->CFG;
		if($this->HTTP->settings){
			$cfg = $this->HTTP->settings;
			$this->set("saved", $this->_writeConfig($cfg) ? true : false);
			$this->_loadConfig(true);
			$cfg = $this->CFG;
		}
	
		$this->set("settings", $cfg);
	}
	
	public function ko_smo(){
		$path = "resource/locale/ko-smo.txt";
		$txt = parse_ini_file($path);
		if($this->HTTP->kosmo){
			$this->set("saved", $this->_writeConfig($this->HTTP->kosmo, $path) ? true : false);
			$txt = $this->HTTP->kosmo;
		}
		$this->set("txt", $txt);
	}
	
	public function reference(){
		$base = self::REFERENCE_BASE;
		
		$file = $_FILES["newimage"];
		$final = array();
		$saved = true;
		if($file && !$file["error"] && preg_match('/\.jpe?g$|\.gif$|\.png$/i', $file["name"])){
			$saved = move_uploaded_file($file["tmp_name"], $base."-1_".$file["name"]);
			$final[] = "-1_".$file["name"];
		}
		$images = self::loadReferenceImages();
		if($this->HTTP->reference){
			$saved = isset($saved) ? $saved : true;
			
			foreach($this->HTTP->reference as $data){
				$f = $data["filename"];
				if(!preg_match('/^'.$data["index"].'_/', $data["filename"]) && isset($data["index"])){
					$f = preg_replace('/^-?\d+_/', $data["index"]."_", $data["filename"]);
					if(!($saved = rename($base.$data["filename"], $base.$f))){
						break;
					}
				}
				$final[] = $f;
			}
			if($saved){
				$dir = opendir($base);
				while($f = readdir($dir)){
					if(!is_dir($base.$f) && !in_array($f, $final)){
						unlink($base.$f);
					}
				}
			}
			$images = self::loadReferenceImages();
			$this->set("saved", $saved);
		}
		$this->set("images", $images);
	}
	public function reference_delete(){
		$menu = $this->loadMenu();
		$path = self::REFERENCE_BASE;
		$dir = opendir($path);
		if($dir){
			while($f = readdir($dir)){
				if(!is_dir($path.$f)){
					unlink($path.$f);
				}
			}
			rmdir($path);
		}
		redirect("/admin");
	}
	
	public static function getAnnounce(){
		$ext = array("jpg", "jpeg", "png", "gif");
		$f = false;
		foreach($ext as $e){
			if(file_exists("resource/image/announce.$e")){
				$f = "/image/announce.$e";
				break;
			}
		}
		return $f;
	}
	
	public function announce(){
		$file = $_FILES["image"];
		if($this->HTTP->announce){
			$saved = true;
			if($this->HTTP->announce["delete"]){
				unlink("resource" . $this->HTTP->announce["delete"]);
			}
			if(!$file["error"]){
				$ex = strtolower(substr($file["name"], strrpos($file["name"], ".")+1));
				if(!move_uploaded_file($file["tmp_name"], "resource/image/announce.$ex")){
					$saved = false;
				}
			}
		}
		
		$this->set("announce", self::getAnnounce());
		$this->set("saved", $saved);
	}
}

?>